import * as THREE from "three"
import {ref,onMounted, computed} from "vue"
import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
import {OrbitControls} from "three/examples/jsm/controls/OrbitControls"
import {RGBELoader} from 'three/examples/jsm/loaders/RGBELoader'
class a{
    text(){
        
      
        //创建场景:
        const scene = new THREE.Scene()
        
        //初始化相机:
        const camera = new THREE.PerspectiveCamera(
          75,window.innerWidth/window.innerHeight,0.1,1000
        )
        camera.position.set(-1, 0, 1);
      scene.add(camera);
        //渲染器:
        const renderer = new THREE.WebGL1Renderer()
      //   renderer.setSize(window.innerWidth,window.innerHeight)
        renderer.setSize(980,550)
        const container = ref(null)
        const render=()=>{
          renderer.render(scene,camera)
          requestAnimationFrame(render)
        }
      
      // const loader = new GLTFLoader()
      const loader = new OBJLoader();
      const textureLoader = new THREE.TextureLoader();
      //颜色贴图:
      const Apple = textureLoader.load(
        "./textures/3DApple001_HQ-2K-JPG/3DApple001_HQ-2K_Color.jpg"
      );
      //黑白贴图:
      const AppleAplhaTexture = textureLoader.load(
        "./textures/3DApple001_HQ-2K-JPG/3DApple001_HQ-2K_AmbientOcclusion.jpg"
      );
      //法线贴图:
      const normalTexture = textureLoader.load(
        "./textures/3DApple001_HQ-2K-JPG/3DApple001_HQ-2K_NormalDX.jpg"
      );
      loader.load("./textures/3DApple001_HQ-2K-JPG/3DApple001_HQ-2K-JPG.obj", function (obj) {
        console.log(obj);
        // const model = gltf.scene
        console.log(obj.children[0].material);
      
        obj.children[0].scale.set(5,5,5); //网格模型缩放
        obj.children[0].geometry.center(); //网格模型的几何体居中
        //   obj.children[0].material.color.set(0xff0000)
        obj.children[0].material.map = Apple;
        obj.children[0].material.alphaMap = AppleAplhaTexture;
        obj.children[0].material.normalMap = normalTexture;
      //   obj.children[0].material.displacementMap=doorHeightTexture
        scene.add(obj);
      });
        
      
      //需要引入RGBELoader的加载器:
     
      //创建球模型:
      const geometry = new THREE.SphereGeometry(5,32,32)
       const loader_1 = new RGBELoader()
       //加载图片,然后实行回调函数 贴图map接收
       loader_1.load('./textures/hdr/002.hdr',(texture) =>{
        const material = new THREE.MeshBasicMaterial({
          map:texture
        })
        //sphere混合模型和材质:
        const sphere = new THREE.Mesh(geometry,material)
        //将z轴倒置进入内部
        sphere.geometry.scale(1,1,-1)
        scene.add(sphere)
       })
        
      let ambientLight = new THREE.AmbientLight(0xffffff,0.7); //设置环境光
      scene.add(ambientLight); //将环境光添加到场景中
      let pointLight = new THREE.PointLight(0xffffff, 0.5, 0.9);
      pointLight.position.set(200, 200, 200); //设置点光源位置
      scene.add(pointLight); //将点光源添加至场景
      
        //挂载完后获取都没对象
        onMounted(()=>{
          //添加轨道控制器:
        const controls = new OrbitControls(camera,container.value)
        controls.enableDamping = true
          container.value.appendChild(renderer.domElement)
          
          render()
        })
       
    }
}
export default a